{{extends file='admin/base.tpl'}}

{{block name='title'}}代码生成器{{/block}}

{{block name='content'}}
<div class="container-fluid">
    <div class="card shadow mb-4">
        <div class="card-header py-3">
            <h6 class="m-0 font-weight-bold text-primary">CRUD代码生成器</h6>
        </div>
        <div class="card-body">
            <form id="generator-form" onsubmit="return false;">
                <div class="form-group">
                    <label for="table_name">选择数据表</label>
                    <select class="form-control" id="table_name" name="table_name" required>
                        <option value="">请选择数据表</option>
                        {{foreach $tables as $table}}
                        <option value="{{$table.name}}">{{$table.display_name}}</option>
                        {{/foreach}}
                    </select>
                </div>
                
                <div class="form-group">
                    <label for="table_display_name">显示名称</label>
                    <input type="text" class="form-control" id="table_display_name" name="table_display_name" placeholder="默认为表名（不含前缀）">
                </div>
                
                <div class="mb-4">
                    <button type="button" id="preview-btn" class="btn btn-info mr-2">预览表结构</button>
                    <button type="button" id="generate-btn" class="btn btn-primary">生成代码</button>
                </div>
            </form>
            
            <!-- 表结构预览 -->
            <div id="table-preview" class="mt-4" style="display: none;">
                <h5>表结构预览</h5>
                <div class="table-responsive">
                    <table class="table table-bordered" id="columnsTable" width="100%" cellspacing="0">
                        <thead>
                            <tr>
                                <th>字段名</th>
                                <th>类型</th>
                                <th>是否为空</th>
                                <th>默认值</th>
                                <th>键</th>
                            </tr>
                        </thead>
                        <tbody id="columns-body">
                        </tbody>
                    </table>
                </div>
            </div>
            
            <!-- 生成结果 -->
            <div id="generate-result" class="mt-4" style="display: none;">
                <h5>生成结果</h5>
                <div class="alert alert-success" id="result-message"></div>
                
                <div class="mt-4">
                    <h6>路由信息（请复制到路由文件）</h6>
                    <pre id="route-info" class="bg-light p-3 rounded"></pre>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
// 预览表结构
$('#preview-btn').click(function() {
    var tableName = $('#table_name').val();
    if (!tableName) {
        alert('请选择数据表');
        return;
    }
    
    $.ajax({
        url: '/admin/generator/getTableInfo',
        type: 'POST',
        data: {table_name: tableName},
        dataType: 'json',
        success: function(res) {
            if (res.code === 200) {
                // 填充表结构
                var columns = res.data.columns;
                var tbody = $('#columns-body');
                tbody.empty();
                
                $.each(columns, function(index, column) {
                    var row = '<tr>' +
                        '<td>' + column.Field + '</td>' +
                        '<td>' + column.Type + '</td>' +
                        '<td>' + column.Null + '</td>' +
                        '<td>' + (column.Default !== null ? column.Default : '') + '</td>' +
                        '<td>' + column.Key + '</td>' +
                        '</tr>';
                    tbody.append(row);
                });
                
                $('#table-preview').show();
            } else {
                alert(res.message);
            }
        },
        error: function() {
            alert('请求失败');
        }
    });
});

// 生成代码
$('#generate-btn').click(function() {
    var tableName = $('#table_name').val();
    var tableDisplayName = $('#table_display_name').val();
    
    if (!tableName) {
        alert('请选择数据表');
        return;
    }
    
    if (confirm('确定要生成代码吗？这将覆盖已存在的同名文件。')) {
        $.ajax({
            url: '/admin/generator/generate',
            type: 'POST',
            data: {
                table_name: tableName,
                table_display_name: tableDisplayName
            },
            dataType: 'json',
            success: function(res) {
                if (res.code === 200) {
                    // 显示生成结果
                    var results = res.data.results;
                    var routeInfo = res.data.route_info;
                    var controllerName = res.data.controller_name;
                    var routeName = res.data.route_name;
                    
                    var message = '<ul>';
                    message += '<li>模型文件: ' + (results.model ? '生成成功' : '生成失败') + '</li>';
                    message += '<li>控制器文件: ' + (results.controller ? '生成成功' : '生成失败') + '</li>';
                    message += '<li>视图文件: ' + (results.views ? '生成成功' : '生成失败') + '</li>';
                    message += '</ul>';
                    message += '<p>生成的控制器: ' + controllerName + '</p>';
                    message += '<p>访问地址: /admin/' + routeName + '</p>';
                    
                    $('#result-message').html(message);
                    $('#route-info').text(routeInfo);
                    $('#generate-result').show();
                } else {
                    alert(res.message);
                }
            },
            error: function() {
                alert('请求失败');
            }
        });
    }
});

// 选择表时自动填充显示名称
$('#table_name').change(function() {
    if (!$('#table_display_name').val()) {
        var selectedOption = $(this).find('option:selected');
        var tableDisplay = selectedOption.text();
        $('#table_display_name').val(tableDisplay);
    }
});
</script>
{{/block}}